/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package computacaografica.formas;

import computacaografica.Janela;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;

/**
 *
 * @author Heitor
 */
public class GraphicsHeitor {

    private Graphics2D g;

    public GraphicsHeitor(Graphics2D graphics) {
        this.g = graphics;

    }

    public void fill(Shape s) {

        AffineTransform old = g.getTransform();
        if (((Formas) s).isCisalhar()) {
            g.shear(Janela.obj().getCisalarX(), Janela.obj().getCisalarY());
        }
        if (((Formas) s).isRotacionar()) {
            g.rotate(Janela.obj().getRotacionar());
        }
        if (((Formas) s).isEscalar()) {
            g.scale(Janela.obj().getEscalarX(), Janela.obj().getEscalarY());
        }
        {
            /*************************************/
            if (s instanceof Oval) {
                Oval oval = (Oval) s;

                if (oval.isPreenche()) {
                    //desenha o circulo preenchido primeiro
                    g.setPaint(oval.getCorPreenchimento());
                    g.fillOval(oval.x, oval.y, oval.width, oval.height);
                    //depois desenha a circulo a linha do circulo;
                    g.setPaint(oval.getCorLinha());
                    g.setStroke(new BasicStroke(oval.getEspessuraLinha()));
                    g.drawOval(oval.x, oval.y, oval.width, oval.height);

                } else {
                    //g.setPaint(null);
                    g.setColor(oval.getCorLinha());
                    g.setStroke(new BasicStroke(oval.getEspessuraLinha()));
                    g.drawOval(oval.x, oval.y, oval.width, oval.height);
                }
            }
            /*************************************/
            if (s instanceof Retangulo) {
                Retangulo ret = (Retangulo) s;

                if (ret.isPreenche()) {
                    g.setPaint(ret.getCorPreenchimento());
                    g.fillRect(ret.x, ret.y, ret.width, ret.height);

                    g.setPaint(ret.getCorLinha());
                    g.setStroke(new BasicStroke(ret.getEspessuraLinha()));
                    g.drawRect(ret.x, ret.y, ret.width, ret.height);

                } else {

                    g.setColor(ret.getCorLinha());
                    g.setStroke(new BasicStroke(ret.getEspessuraLinha()));
                    g.drawRect(ret.x, ret.y, ret.width, ret.height);

                }
            }
            /*************************************/
            if (s instanceof RetanguloRound) {
                RetanguloRound retRound = (RetanguloRound) s;

                if (retRound.isPreenche()) {
                    g.setPaint(retRound.getCorPreenchimento());
                    g.fillRoundRect(retRound.x, retRound.y, retRound.width, retRound.height, retRound.getArcWidth(), retRound.getArcHeigh());

                    g.setPaint(retRound.getCorLinha());
                    g.setStroke(new BasicStroke(retRound.getEspessuraLinha()));
                    g.drawRoundRect(retRound.x, retRound.y, retRound.width, retRound.height, retRound.getArcWidth(), retRound.getArcHeigh());

                } else {
                    g.setColor(retRound.getCorLinha());
                    g.setStroke(new BasicStroke(retRound.getEspessuraLinha()));
                    g.drawRoundRect(retRound.x, retRound.y, retRound.width, retRound.height, retRound.getArcWidth(), retRound.getArcHeigh());
                }
            }
            /*************************************/
            if (s instanceof Reta) {
                Reta reta = (Reta) s;
                g.setColor(reta.getCorLinha());
                g.setStroke(new BasicStroke(reta.getEspessuraLinha()));
                g.drawLine(reta.x, reta.y, reta.width, reta.height);

            }
            /*************************************/
            if (s instanceof Triangulo) {
                Triangulo triangulo = (Triangulo) s;
                g.setColor(triangulo.getCorLinha());
                if (triangulo.isPreenche()) {
                    g.setPaint(triangulo.getCorPreenchimento());
                    g.fillPolygon(triangulo);
                    g.setPaint(triangulo.getCorLinha());
                    g.setStroke(new BasicStroke(triangulo.getEspessuraLinha()));
                    g.drawPolygon(triangulo);

                } else {
                    g.setColor(triangulo.getCorLinha());
                    g.setStroke(new BasicStroke(triangulo.getEspessuraLinha()));
                    g.drawPolygon(triangulo);
                }
            }
            /*************************************/
            if (s instanceof TrianguloRet) {
                TrianguloRet trianguloRet = (TrianguloRet) s;

                if (trianguloRet.isPreenche()) {

                    g.setPaint(trianguloRet.getCorLinha());
                    g.setStroke(new BasicStroke(trianguloRet.getEspessuraLinha()));
                    g.drawPolygon(trianguloRet);

                } else {
                    g.setColor(trianguloRet.getCorLinha());
                    g.setStroke(new BasicStroke(trianguloRet.getEspessuraLinha()));
                    g.drawPolygon(trianguloRet);
                }
            }
        }
        
        if (((Formas) s).isCisalhar()) {
            g.shear(-Janela.obj().getCisalarX(), -Janela.obj().getCisalarY());
        }
        if (((Formas) s).isRotacionar()) {
            g.rotate(-Janela.obj().getRotacionar());
        }
        if (((Formas) s).isEscalar()) {
            g.scale(-Janela.obj().getEscalarX(), -Janela.obj().getEscalarY());
        }
        g.setTransform(old);
    }

    public Graphics2D getGraphics() {
        return g;
    }

    /**
     * Método para transladar
     * @param Tx
     * @param Ty
     * @param s 
     * @return 
     * @author Heitor
     */
    public Shape Transladar(int Tx, int Ty, Shape s) {
        // como a reta são 2 pontos é necessario tratar ela separadamente
        if (s instanceof Reta) {
            Reta t = (Reta) s;
            //seta o primeiro ponto
            t.x += Tx;
            t.y += Ty;
            //seta o segundo ponto
            t.height += Tx;
            t.width += Ty;

            return t;
        }
        if (s instanceof Rectangle) {

            Rectangle r = (Rectangle) s;
            r.x += Tx;
            r.y += Ty;

            return r;
        }
        if (s instanceof Polygon) {
            Polygon p = (Polygon) s;
            int i = 0;
            for (i = 0; i < p.xpoints.length; i++) {
                p.xpoints[i] += Tx;
                p.ypoints[i] += Ty;
            }

            return p;
        }
        return null;
    }

    /**
//     * Método para cisalhar
//     * @param Shx
//     * @param Shy 
//     * @param s 
//     * @return 
//     * @author Heitor
//     */
//    public Shape Cisalhar(int Shx, int Shy, Shape s) {
//
//        if (s instanceof Reta) {
//
//            return s;
//        }
//
//        if (s instanceof Rectangle) {
//            Rectangle r = (Rectangle) s;
//
//            r.x += Shx * r.y;
//            //r.y += y;
//            return r;
//        }
//        if (s instanceof Polygon) {
//            Polygon p = (Polygon) s;
//            int i = 0;
//            for (i = 0; i < p.xpoints.length; i++) {
//                p.xpoints[i] += Shx * p.xpoints[i];
//                p.ypoints[i] += Shy * p.ypoints[i];
//            }
//            return p;
//        }
//        return null;
//    }
//
//    /**
//     * Método para escalar
//     * @param Sx
//     * @param Sy
//     * @param s  
//     */
//    public void Escalar(int Sx, int Sy, Shape s) {
//    }
//
//    /**
//     * Método para rotacionar
//     * @param theta
//     * @param s  
//     */
//    public void Rotacionar(int theta, Shape s) {
//    }
}
